﻿using System;
using System.Collections.Generic;
using Coolite.Ext.Web;
using Pap.Web.Modules.Membership.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Membership.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UsersList : BasePage<UsersListViewModel>, IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {

            if (this.ViewModel != null)
            {
                UsersStore.DataSource = this.ViewModel.SelectAllUsers();
                UsersStore.DataBind();
            }
        }

        /// <summary>
        /// Handles the Command event of the gvUsers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void gvUsers_Command(object sender, AjaxEventArgs e)
        {
            if (e.ExtraParams["CommandName"].Equals("Edit"))
            {
                RowSelectionModel model = gvUsers.SelectionModel.Primary as RowSelectionModel;
                Guid userId = e.ExtraParams["ID"].ToString().ToGuid();
                Ext.Redirect(string.Format("~/Membership.route/UserDetails?UserID={0}", userId), GetLocalResourceObject("MSGWorking").ToString());
            }
        }

        /// <summary>
        /// Deletes the selected users.
        /// </summary>
        [AjaxMethod]
        public void DeleteSelectedUsers()
        {
            try
            {
                SelectedRowCollection selectedRows = (gvUsers.SelectionModel.Primary as RowSelectionModel).SelectedRows;
                List<Guid> selectedUsers = new List<Guid>();
                foreach (var row in selectedRows)
                {
                    selectedUsers.Add(new Guid(row.RecordID));
                }
                this.ViewModel.DeleteAll(selectedUsers);
                //---------------------------------------------------------------
                UsersStore.DataSource = this.ViewModel.SelectAllUsers();
                UsersStore.DataBind();
                gvUsers.Reload();
            }
            catch
            {
                Ext.Msg.Configure(new MessageBox.Config
                {
                    Title = GetLocalResourceObject("MBError").ToString(),
                    Message = GetLocalResourceObject("MBCouldntDelete").ToString(),
                    Icon = MessageBox.Icon.ERROR,
                    Buttons = MessageBox.Button.OK
                }).Show();
            }
        }


        #region IPermittedPage Members

        /// <summary>
        /// Gets the name of the permission.
        /// </summary>
        /// <value>
        /// The name of the permission.
        /// </value>
        public string PermissionName
        {
            get { return "UsersList"; }
        }

        #endregion
    }
}
